package com.yztc.youbadbad.ui.other.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.MyPagerAdapter;
import com.yztc.youbadbad.base.BaseFragment;
import com.yztc.youbadbad.ui.homepage.HaowuHomeFragment;
import com.yztc.youbadbad.ui.homepage.OtherHomeFragment;
import com.yztc.youbadbad.ui.homepage.TuijianHomeFragment;
import com.yztc.youbadbad.ui.homepage.ZhuyangHomeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页fragment
 * getMyFragment得到Activity
 */
public class HomepageFragment extends BaseFragment {
    @BindView(R.id.tablayout_home_fg)
    TabLayout tablayoutHomeFg;
    @BindView(R.id.viewpager_home_fg)
    ViewPager viewpagerHomeFg;

    private Context mContext = getActivity();
    private String[] mTabArr;//顶部导航的标题数组
    private List<Fragment> mTabList = new ArrayList<>();
    private int[] mTabId;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDataAndView();
    }

    private void initDataAndView() {
        mTabArr = getResources().getStringArray(R.array.home_tab);//名字
        mTabId = getResources().getIntArray(R.array.home_tabId);//后面fragment复用id
        for (int i = 0; i < mTabArr.length; i++) {
            Fragment fragment;
            if (i == 0){
                fragment = new TuijianHomeFragment();
            }else if (i == 1){
                fragment = new HaowuHomeFragment();
            }else  if (i == 2){
                fragment = new ZhuyangHomeFragment();
            }else {
                fragment = OtherHomeFragment.newInstance(mTabId[i]);
            }
            mTabList.add(fragment);
        }
        //初始化视图
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), mTabList, mTabArr);
        viewpagerHomeFg.setAdapter(adapter);
        tablayoutHomeFg.setupWithViewPager(viewpagerHomeFg);//tabLayout绑定viewPager

    }
}
